<template>
    <rx-dialog @ok="handleCompleteCheck" @cancel="closeWindow" :okText="$ti18('rxForm.commitBtn','提交')"
               :btnAlign="'center'">
        <rx-fit>

            <template #middle>
                <div style="width:100%;padding:10px;">
                    <a-form ref="form" :model="form" :rules="rules" layout="vertical" class="rx-a-form-vertical">
                        <a-form-item v-if="rejectOptions && rejectOptions.length>1" @change="handleCheckTypeChange"
                                     :label="i18n('rejectPage.rejectMode','驳回方式')" name="checkType">
                            <a-radio-group v-model:value="form.checkType" :options="rejectOptions">
                            </a-radio-group>
                        </a-form-item>

                        <a-form-item v-if="form.checkType=='BACK_SPEC'" :label="i18n('rejectPage.specifyNodes','指定节点')">
                            <a-select v-model:value="form.destNodeId" style="width:100%">
                                <a-select-option v-for="item in backNodes" :key="item.value" :value="item.value">
                                    {{ item.label }}
                                </a-select-option>
                            </a-select>
                        </a-form-item>

                        <a-form-item
                            v-if="form.checkType=='BACK_TO_STARTOR' && !isBatch && (taskConfig.startReturnMode|| 'both' =='both')"
                            :label="i18n('rejectPage.backTaskComplete','回退任务完成后')">
                            <a-radio-group v-model:value="form.nextJumpType">
                                <a-radio value="normal">{{ i18n('rejectPage.normal', '正常执行') }}</a-radio>
                                <a-radio value="source">{{ i18n('rejectPage.backtrack', '原路返回') }}</a-radio>
                            </a-radio-group>
                        </a-form-item>

                        <a-form-item v-if="form.checkType=='BACK_SPEC' && !isBatch && (taskConfig.specReturnMode|| 'both'=='both')"
                                     :label="i18n('rejectPage.backTaskComplete','回退任务完成后')">
                            <a-radio-group v-model:value="form.nextJumpType">
                                <a-radio value="normal">{{ i18n('rejectPage.normal', '正常执行') }}</a-radio>
                                <a-radio value="source">{{ i18n('rejectPage.backtrack', '原路返回') }}</a-radio>
                            </a-radio-group>
                        </a-form-item>

                        <a-form-item :label="i18n('taskCheck.opinion','意见')" name="opinion">
                            <a-textarea :placeholder="i18n('taskCheck.opinion','请填写意见','write')"
                                        v-model:value="form.opinion"></a-textarea>
                        </a-form-item>

                        <a-form-item :label="i18n('taskCheck.attachment','附件')">
                            <rx-attach-component v-model="form.fileList" class="uploader-btn-ctl"></rx-attach-component>
                        </a-form-item>

                        <a-form-item :label="i18n('taskCheck.cc','抄送')">
                            <rx-user-component v-model="form.copyUsers" :isAccount="true"
                                               :single="false"></rx-user-component>
                        </a-form-item>

                    </a-form>
                </div>
            </template>
        </rx-fit>
    </rx-dialog>
</template>
<script>
import {RxDialog, Util, RxFit} from '@lowcode/jpaas-base-lib'
// import {RxDialog, Util, RxFit, RxTextBoxList} from '@lowcode/jpaas-base-lib'
import {RxAttachComponent, RxUserComponent} from '@lowcode/jpaas-share-component';
import BpmtaskApi from '@/common/api/bpm/bpmTask'
import BpmTaskExecutors from "../../components/BpmTaskExecutors.vue";

export default {
    name: 'bpm-task-back',
    components: {
        RxFit,
        // RxTextBoxList,
        RxDialog,
        RxUserComponent,
        RxAttachComponent,
        BpmTaskExecutors
    },
    props: {
        taskId: {
            type: String,
            required: true
        },
        instId: {
            type: String,
            required: true
        },
        defId: {
            type: String,
            required: true
        },
        formJson: String,
        layerid: String,
        destroy: Function,
        taskConfig: Object,
        isBatch: Boolean,
        opinionName: {
            type: String,
            default: ""
        }
    },
    data() {
        return {
            rules: {
                opinion: [{required: true, message: this.i18n('form.tipOpinion', '请填写意见!'), trigger: 'change'}],
                checkType: [{
                    required: true,
                    message: this.i18n('rejectPage.rejectMode', '驳回方式', 'select'),
                    trigger: 'change'
                }],
            },
            labelCol: {span: 4},
            wrapperCol: {span: 8},

            backConfig: [
                {label: this.i18n('rejectPage.backPreStep', '驳回上一步'), value: 'BACK'},
                {label: this.i18n('rejectPage.backToPromoter', '驳回到发起人'), value: 'BACK_TO_STARTOR'},
                {label: this.i18n('rejectPage.backToSpecified', '驳回到审批节点'), value: 'BACK_SPEC'}
            ],
            rejectOptions: [],
            backNodes: [],
            checkHistorys: [],
            form: {
                checkType: 'BACK',
                nextJumpType: 'normal',
                destNodeId: '',
                opinion: '',
                fileList: [],
                copyUsers: [],
            }
        }
    },
    created() {
        let backsTmp = this.taskConfig.backOptions;

        if (this.isBatch) {
            backsTmp = ['BACK_TO_STARTOR'];
        }
        let tmp = [];
        for (let i = 0; i < this.backConfig.length; i++) {
            if (backsTmp.contains(this.backConfig[i].value)) {
                tmp.push(this.backConfig[i]);
            }
        }
        this.form.checkType = tmp[0].value;
        this.rejectOptions = tmp;

        //驳回类型
        this.initReturnMode();

        this.initBackNodes();
    },
    methods: {
        initReturnMode() {
            let startReturnMode = this.taskConfig.startReturnMode || "both";
            let specReturnMode = this.taskConfig.specReturnMode || "both";
            if (this.form.checkType == "BACK_TO_STARTOR") {
                if (startReturnMode && startReturnMode != 'both') {
                    this.form.nextJumpType = startReturnMode;
                }
            }

            if (this.form.checkType == "BACK_SPEC") {
                if (specReturnMode && specReturnMode != 'both') {
                    this.form.nextJumpType = specReturnMode;
                }
            }
        },
        handleCheckTypeChange() {
            this.initReturnMode();
        },
        i18n(name, text, key) {
            return this.$ti18(name, text, "bpmTask", key);
        },
        initBackNodes() {
            let self_ = this;
            BpmtaskApi.getBackNodes(this.taskId).then(res => {
                self_.backNodes = res.map(item => {
                    return {label: item.nodeName, value: item.nodeId};
                })
            });
        },
        handleCompleteCheck(vm) {
            let self = this;
            this.$refs.form.validate().then(() => {
                //抄送用户
                let copyUserIds = self.getCopyUserAccounts();

                let cmd = {
                    taskId: self.taskId,
                    checkType: self.form.checkType,
                    nextJumpType: self.form.nextJumpType,
                    opinion: self.form.opinion.trim(),

                    copyUserAccounts: copyUserIds,
                    formJson: self.formJson,
                    opinionName: self.opinionName,
                    opFiles: JSON.stringify(self.form.fileList)
                }

                if (self.form.checkType == 'BACK_SPEC') {
                    if (!self.form.destNodeId) {
                        self.$message.warn(self.i18n('rejectPage.specifyNode', '请指定节点！'));
                        vm.loading = false;
                        return;
                    }
                    cmd.destNodeId = self.form.destNodeId;
                }
                BpmtaskApi.checkComplete(cmd).then(resp => {
                    vm.loading = false;
                    if (resp.success) {
                        Util.closeWindow(self, 'ok');
                    }
                })
            }).catch((error) => {
                vm.loading = false;
            })
        },
        /**
         * 获取抄送用户。
         * @returns {string}
         */
        getCopyUserAccounts() {
            var accounts = [];
            if (this.form.copyUsers && this.form.copyUsers.length > 0) {
                this.form.copyUsers.forEach(item => {
                    accounts.push(item.account);
                })
            }
            return accounts.join(",");
        },
        closeWindow() {
            Util.closeWindow(this, 'cancel')
        }
    }
}
</script>
<style scoped>
.uploader-btn-ctl ::v-deep(.uploader-btn){
    padding: 5px 6px;
}
</style>
